package manage.dao;

import m.common.dao.Dao;
import m.system.db.DBManager;
import m.system.db.DataRow;

import java.sql.SQLException;

public class AdminLoginDao extends Dao {
	/**
	 * 根据用户名oid  外协登录使用
	 */
	public String getUserOid4Username(String username) throws SQLException{
		DataRow dr=DBManager.queryFirstRow("select oid from os_admin_login where username=?",username);
		if(null!=dr){
			return dr.get(String.class,"oid");
		}else{
			return null;
		}
	}
	/**
	 * 根据用户名和密码获取oid
	 *  username
	 *  password
	 * 
	 *  SQLException
	 */
	public String getUserOid(String username,String password) throws SQLException{
		DataRow dr=DBManager.queryFirstRow("select oid from os_admin_login where username=? and password=?",username,password);
		if(null!=dr){
			return dr.get(String.class,"oid");
		}else{
			return null;
		}
	}
	/**
	 * 根据token获取用户oid
	 *  token
	 * 
	 *  SQLException 
	 */
	public String getUserOid(String token) throws SQLException {
		DataRow dr=DBManager.queryFirstRow("select oid from os_admin_login where token=?",token);
		if(null!=dr){
			return dr.get(String.class,"oid");
		}else{
			return null;
		}
	}
	/**
	 * 更新用户的当前机构，只更新为空的
	 *  SQLException
	 */
	public void updateOrgGroup4Null() throws SQLException {
		DBManager.executeUpdate("update os_admin_login al set al.org_group_oid=( " + 
				" SELECT max(gl.admin_group_oid) FROM os_admin_group_link gl WHERE gl.admin_oid=al.oid and gl.type='C' " + 
				") where al.org_group_oid='' or al.org_group_oid is null");
	}
}
